package com.example.blog.modules.monitor.controller;

import com.example.blog.common.api.ApiResponse;
import com.example.blog.common.security.HasPermission;
import com.example.blog.modules.monitor.dto.SystemMetricsDTO;
import com.example.blog.modules.monitor.dto.MetricsHistoryDTO;
import com.example.blog.modules.monitor.dto.MetricsStatisticsDTO;
import com.example.blog.modules.monitor.service.SystemMonitorService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Tag(name = "系统监控", description = "系统监控相关接口")
@RestController
@RequestMapping("/api/monitor")
@RequiredArgsConstructor
public class SystemMonitorController {
    
    private final SystemMonitorService systemMonitorService;
    
    @Operation(summary = "获取系统指标")
    @GetMapping("/metrics")
    @HasPermission("system:monitor:metrics")
    public ApiResponse<SystemMetricsDTO> getSystemMetrics() {
        return ApiResponse.success(systemMonitorService.getSystemMetrics());
    }
    
    @Operation(summary = "获取在线用户数")
    @GetMapping("/online-users")
    @HasPermission("system:monitor:online")
    public ApiResponse<Long> getOnlineUserCount() {
        return ApiResponse.success(systemMonitorService.getOnlineUserCount());
    }
    
    @Operation(summary = "获取系统信息")
    @GetMapping("/system-info")
    @HasPermission("system:monitor:info")
    public ApiResponse<Map<String, String>> getSystemInfo() {
        return ApiResponse.success(systemMonitorService.getSystemInfo());
    }
    
    @Operation(summary = "获取Redis信息")
    @GetMapping("/redis-info")
    @HasPermission("system:monitor:redis")
    public ApiResponse<Map<String, Object>> getRedisInfo() {
        return ApiResponse.success(systemMonitorService.getRedisInfo());
    }
    
    @Operation(summary = "获取数据库信息")
    @GetMapping("/database-info")
    @HasPermission("system:monitor:database")
    public ApiResponse<Map<String, Object>> getDatabaseInfo() {
        return ApiResponse.success(systemMonitorService.getDatabaseInfo());
    }
    
    @Operation(summary = "获取监控历史记录")
    @GetMapping("/history")
    @HasPermission("system:monitor:history")
    public ApiResponse<List<MetricsHistoryDTO>> getMetricsHistory(
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime start,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime end) {
        return ApiResponse.success(systemMonitorService.getMetricsHistory(start, end));
    }
    
    @Operation(summary = "获取最近的监控记录")
    @GetMapping("/recent")
    @HasPermission("system:monitor:history")
    public ApiResponse<List<MetricsHistoryDTO>> getRecentMetrics(@RequestParam(defaultValue = "60") int count) {
        return ApiResponse.success(systemMonitorService.getRecentMetrics(count));
    }
    
    @Operation(summary = "获取监控统计信息")
    @GetMapping("/statistics")
    @HasPermission("system:monitor:statistics")
    public ApiResponse<MetricsStatisticsDTO> getMetricsStatistics(
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime start,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime end) {
        return ApiResponse.success(systemMonitorService.getMetricsStatistics(start, end));
    }
    
    @Operation(summary = "清理历史记录")
    @DeleteMapping("/history")
    @HasPermission("system:monitor:cleanup")
    public ApiResponse<Void> cleanupHistory(
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime before) {
        systemMonitorService.cleanupHistory(before);
        return ApiResponse.success();
    }
} 